from frame.api.admin.base_admin import BaseAdmin


class GoodsManage(BaseAdmin):
    goods_url = "goods/"

    def create_goods(self, goods_name):
        """
        添加商品类目接口的实现
        :return:
        """
        # 请求数据
        create_data = {
            "goods": {"picUrl": "", "gallery": [], "isHot": False, "isNew": True, "isOnSale": True, "goodsSn": "100",
                      "name": goods_name}, "specifications": [{"specification": "规格", "value": "标准", "picUrl": ""}],
            "products": [{"id": 0, "specifications": ["标准"], "price": "100", "number": "200", "url": ""}],
            "attributes": []}
        # 定义请求参数
        create_req = {
            "url": self.goods_url + "create",
            "method": "POST",
            "json": create_data
        }
        # 发起请求
        create_res = self.send_api(create_req)
        return create_res.json()

    def delete_goods(self, goods_name, goods_id):
        """
        添加商品类目接口的实现
        :return:
        """
        # 请求数据
        del_data = {"id": goods_id, "goodsSn": "110", "name": goods_name, "categoryId": 0, "brandId": 0, "gallery": [],
                    "keywords": "", "brief": "", "isOnSale": True, "sortOrder": 100, "picUrl": "", "isNew": True,
                    "isHot": False, "unit": "’件‘", "counterPrice": 0, "retailPrice": 100,
                    "addTime": "2022-09-15 06:04:58", "updateTime": "2022-09-15 06:04:58", "deleted": False,
                    "preview": [""]}
        # 定义请求参数
        del_req = {
            "url": self.goods_url + "delete",
            "method": "POST",
            "json": del_data
        }
        # 发起请求
        del_res = self.send_api(del_req)
        return del_res.json()

    def list_goods(self, goods_name):
        """
        展示商品列表
        """

        list_data = {
            "page": 1,
            "limit": 20,
            "name": goods_name
        }

        # 定义请求参数
        list_req = {
            "url": self.goods_url + "list",
            "method": "get",
            "params": list_data
        }
        # 发起请求
        list_res = self.send_api(list_req)
        return list_res.json()
